package com.wjq.demo.redisson;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @author wjq
 * @since 2023-02-08
 */
public class RedissonTest {

    static Logger logger = LoggerFactory.getLogger(RedissonTest.class);

    public static void main(String[] args) throws InterruptedException {

        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://121.37.96.180:6379");

        RedissonClient client = Redisson.create(config);

        RLock lock = client.getLock("test1111");

        //TimeUnit.SECONDS.sleep(20);
        boolean b = lock.tryLock(3, -1, TimeUnit.SECONDS);
        if (b) {
           // logger.info("get the lock");
            TimeUnit.SECONDS.sleep(20);
        }
        if (lock.isLocked()) {
            logger.info("the lock still locked");
        }
        lock.unlock();
        logger.info("unlock ,the lock status:{}", lock.isLocked());
    }
}
